package scuba.DiveBuddy;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TextView;
import org.joda.time.DateTime;
import scuba.DiveBuddy.Class.DiverProfile;

import java.util.Calendar;

/**
 * @Author Alex Anderson
 * @Version 1
 * @Since 22/01/14.
 */
public class EditProfileActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profileeditor);

        //Fill in Data
        UpdateGUI();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.savecancel_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
            case R.id.action_save:
                onEditClick();
                break;

            default:
                ReturnToHome();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public void ReturnToHome() {
        // Remove activity from the stack, close it :)
        finish();
    }

    public void UpdateGUI() {
        DiverProfile active = DiverProfile.getActiveProfile();

        //forename
        TextView v = (TextView)findViewById(R.id.diverForename);
        v.setText(active.getForename());

        //surname
        v = (TextView)findViewById(R.id.diverSurname);
        v.setText(active.getSurname());

        //Date of Birth
        DatePicker vdob  = (DatePicker)findViewById(R.id.profileDOB);
        Calendar dob = Calendar.getInstance();
        dob.setTime(active.getDOB().toDate());
        vdob.updateDate(dob.get(Calendar.YEAR), dob.get(Calendar.MONTH),dob.get(Calendar.DAY_OF_MONTH));

        //ice Contact
        v = (TextView)findViewById(R.id.diverICEContact);
        v.setText(active.getICEContact());

        //ICE Number
        v = (TextView)findViewById(R.id.diverICENumber);
        v.setText(active.getICENumber());
    }

    public void onEditClick() {
        DiverProfile active = DiverProfile.getActiveProfile();

        //Simply using the set method will be enough, it will check before going any deeper into the database
        String forename, surname, icecontact, icenumber;

        try {
            forename = ((TextView)findViewById(R.id.diverForename)).getText().toString();
            active.setForename(forename);

            surname = ((TextView)findViewById(R.id.diverSurname)).getText().toString();
            active.setSurname(surname);

            icecontact = ((TextView)findViewById(R.id.diverICEContact)).getText().toString();
            active.setICEContact(icecontact);

            icenumber = ((TextView)findViewById(R.id.diverICENumber)).getText().toString();
            active.setICENumber(icenumber);
        }
        catch(NullPointerException n) {
            //something was null
            Log.e("INTERFACE", "Exception thrown in EditProfileActivity::onEditClick \n " + n.getMessage());
        }

        DatePicker dp = (DatePicker)findViewById(R.id.profileDOB);
        Calendar dob = Calendar.getInstance();
        dob.set(dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
        active.setDOB(new DateTime(dob.getTime()));

        ReturnToHome();
    }
}